﻿using System;
using System.Linq;
using System.Collections.Generic;
using VirtualTournaments.Biz.Tournaments;

namespace VirtualTournaments.Data.DataMappers
{
    public class TournamentsDataMapper : AbstractDataMapper<Tournament>
    {
        private static TournamentsDataMapper _instance = null;
        private static object _instanceLocker = new object();

        private TournamentsDataMapper()
        {
        }

        public static TournamentsDataMapper Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_instanceLocker)
                    {
                        if (_instance == null)
                        {
                            _instance = new TournamentsDataMapper();
                        }
                    }
                }
                return _instance;
            }
        }

        protected internal override IQueryable<Tournament> GetDataSource()
        {
            List<Tournament> source = new List<Tournament>();
            source.Add
            (
                new Tournament()
                {
                    IsActive = true,
                    Name = "TorneoRaul"
                }
            );
            source.Add
            (
                new Tournament()
                {
                    IsActive = true,
                    Name = "TorneoVane",
                }
            );
            source.Add
            (
                new Tournament()
                {
                    IsActive = false,
                    Name = "TorneoNahuel",
                }
            );


            var torneos = from torneo in source.AsQueryable()
                          where torneo.IsActive == true || torneo.Name == "Vane"
                          select torneo;



            return source.AsQueryable();
        }

        public IEnumerable<Tournament> GetAll()
        {
            return base.GetList().AsEnumerable();
        }

        public IEnumerable<Tournament> GetActives()
        {
            return base.GetList(tournament => tournament.IsActive).AsEnumerable();
        }

        public IEnumerable<Tournament> GetByName(string name)
        {
            return base.GetList(tournament => tournament.Name.ToLower().Contains(name.ToLower())).AsEnumerable();
        }
    }
}
